package com.huawei.hms.push.ups;

import android.content.Context;
import com.huawei.hms.aaid.HmsInstanceId;
import com.huawei.hms.aaid.constant.AaidIdConstant;
import com.huawei.hms.common.ApiException;
import com.huawei.hms.common.internal.Preconditions;
import com.huawei.hms.push.HmsMessaging;
import com.huawei.hms.push.ups.entity.CodeResult;
import com.huawei.hms.push.ups.entity.TokenResult;
import com.huawei.hms.push.ups.entity.UPSRegisterCallBack;
import com.huawei.hms.push.ups.entity.UPSTurnCallBack;
import com.huawei.hms.push.ups.entity.UPSUnRegisterCallBack;
import com.huawei.hms.support.api.push.c.a;
import com.huawei.hms.support.log.HMSLog;
import o.fjo;
import o.fjq;

public final class UPSService {

    final class AnonymousClass1 implements fjq<Void> {
        final /* synthetic */ UPSTurnCallBack a;

        AnonymousClass1(UPSTurnCallBack uPSTurnCallBack) {
            this.a = uPSTurnCallBack;
        }

        public void onComplete(fjo<Void> o_fjo_java_lang_Void) {
            if (o_fjo_java_lang_Void.ˊ()) {
                this.a.onResult(new CodeResult());
                return;
            }
            ApiException apiException = (ApiException) o_fjo_java_lang_Void.ˋ();
            this.a.onResult(new CodeResult(apiException.getStatusCode(), apiException.getMessage()));
        }
    }

    final class AnonymousClass2 implements fjq<Void> {
        final /* synthetic */ UPSTurnCallBack a;

        AnonymousClass2(UPSTurnCallBack uPSTurnCallBack) {
            this.a = uPSTurnCallBack;
        }

        public void onComplete(fjo<Void> o_fjo_java_lang_Void) {
            if (o_fjo_java_lang_Void.ˊ()) {
                this.a.onResult(new CodeResult());
                return;
            }
            ApiException apiException = (ApiException) o_fjo_java_lang_Void.ˋ();
            this.a.onResult(new CodeResult(apiException.getStatusCode(), apiException.getMessage()));
        }
    }

    private UPSService() {
    }

    public static void registerToken(Context context, String str, String str2, String str3, UPSRegisterCallBack uPSRegisterCallBack) {
        HMSLog.i("UPSService", "invoke registerToken");
        Preconditions.checkNotNull(uPSRegisterCallBack);
        if (a.a()) {
            try {
                uPSRegisterCallBack.onResult(new TokenResult(HmsInstanceId.getInstance(context).getToken(str, AaidIdConstant.DEFAULT_SCOPE_TYPE)));
                return;
            } catch (ApiException e) {
                uPSRegisterCallBack.onResult(new TokenResult(e.getStatusCode(), e.getMessage()));
                return;
            }
        }
        uPSRegisterCallBack.onResult(new TokenResult(com.huawei.hms.push.a.a.ERROR_OPERATION_NOT_SUPPORTED.b()));
    }

    public static void unRegisterToken(Context context, UPSUnRegisterCallBack uPSUnRegisterCallBack) {
        HMSLog.i("UPSService", "invoke unRegisterToken");
        Preconditions.checkNotNull(uPSUnRegisterCallBack);
        if (a.a()) {
            try {
                HmsInstanceId.getInstance(context).deleteToken(null, null);
                uPSUnRegisterCallBack.onResult(new TokenResult());
                return;
            } catch (ApiException e) {
                uPSUnRegisterCallBack.onResult(new TokenResult(e.getStatusCode(), e.getMessage()));
                return;
            }
        }
        uPSUnRegisterCallBack.onResult(new TokenResult(com.huawei.hms.push.a.a.ERROR_OPERATION_NOT_SUPPORTED.b()));
    }

    public static void turnOnPush(Context context, UPSTurnCallBack uPSTurnCallBack) {
        HMSLog.i("UPSService", "invoke turnOnPush");
        Preconditions.checkNotNull(uPSTurnCallBack);
        if (a.a()) {
            HmsMessaging.getInstance(context).turnOnPush().ˏ(new AnonymousClass1(uPSTurnCallBack));
        } else {
            uPSTurnCallBack.onResult(new TokenResult(com.huawei.hms.push.a.a.ERROR_OPERATION_NOT_SUPPORTED.b()));
        }
    }

    public static void turnOffPush(Context context, UPSTurnCallBack uPSTurnCallBack) {
        HMSLog.i("UPSService", "invoke turnOffPush");
        Preconditions.checkNotNull(uPSTurnCallBack);
        if (a.a()) {
            HmsMessaging.getInstance(context).turnOffPush().ˏ(new AnonymousClass2(uPSTurnCallBack));
        } else {
            uPSTurnCallBack.onResult(new TokenResult(com.huawei.hms.push.a.a.ERROR_OPERATION_NOT_SUPPORTED.b()));
        }
    }
}
